/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import com.jnetx.javax.sip.util.ByteArrayUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.ParseException;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class RetryAfterHeaderParser extends ParametrizedHeaderParserBase<RetryAfterHeaderImpl> {
    private final static Log logger = LogFactory.getLog(RetryAfterHeaderParser.class);

    protected Log getLogger() {
        return logger;
    }

    public RetryAfterHeaderImpl createLazily(byte[] data, int start, int end) {
        return new RetryAfterHeaderImpl(data, start, end);
    }

    //Retry-After: 18000;duration=3600
    //Retry-After: 120 (I'm in a meeting)
    public RetryAfterHeaderImpl[] parse(RetryAfterHeaderImpl header, byte[] data, int start, int end) throws ParseException {
        if (header == null) header = new RetryAfterHeaderImpl();
        final int startOfData = ByteArrayUtil.firstMissing(data, ' ', start, end);
        int endOfPart = ByteArrayUtil.lastMissing(data, ' ', startOfData, end);
        if (endOfPart == -1)
            fail("Cannot parse the Retry-After header!", startOfData);
        else
            endOfPart++;
        final int startOfParameter = ByteArrayUtil.firstOccurence(data, ';', startOfData, endOfPart);
        int endOfData = (startOfParameter == -1) ? endOfPart : startOfParameter;
        int endOfRetry = ByteArrayUtil.firstOccurence(data, ' ', startOfData, endOfData);
        if (endOfRetry==-1)
            endOfRetry = startOfParameter;
        if (endOfRetry != -1) {
            int startOfComment = ByteArrayUtil.firstOccurence(data, '(', endOfRetry + 1, endOfData);
            if (startOfComment != -1) {
                startOfComment++;
                final int endOfComment = ByteArrayUtil.lastOccurence(data, ')', startOfComment + 1, endOfData);
                if (endOfComment == -1)
                    fail("Cannot parse a comment in the Retry-After header!", startOfComment);
                header.setComment(new String(data, startOfComment, endOfComment - startOfComment));
            }
        }
        try {
            final int retryAfter = Integer.parseInt(new String(data, startOfData, endOfRetry - startOfData));
            header.setRetryAfter(retryAfter);
        } catch (Exception e) {
            fail("Cannot parse the Retry-After header!", startOfData);
        }
        if (startOfParameter != -1)
            parseParameters(header, data, startOfParameter, endOfPart);
        return new RetryAfterHeaderImpl[]{header};
    }
}
/*
 * $Log$
 */